package org.jeecg.modules.nikeProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.nikeProject.entity.SportsProject;
import org.jeecg.modules.nikeProject.entity.SportsScoreRule;
import org.jeecg.modules.nikeProject.mapper.SportsProjectMapper;
import org.jeecg.modules.nikeProject.mapper.SportsScoreRuleMapper;
import org.jeecg.modules.nikeProject.service.ISportsScoreRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 评分规则表
 * @Author: jeecg-boot
 * @Date:   2025-02-12
 * @Version: V1.0
 */
@Service
@Slf4j
public class SportsScoreRuleServiceImpl extends ServiceImpl<SportsScoreRuleMapper, SportsScoreRule> implements ISportsScoreRuleService {

    @Autowired
    private SportsProjectMapper sportsProjectMapper;
    @Autowired
    private SportsScoreRuleMapper sportsScoreRuleMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Result<?> queryList(String year, String projectId) {
        Map<String, Object> map = new HashMap<>();
        if ("".equals(projectId) && null == projectId){
            return Result.error("请告知案例编号，否则无法查询具体的评分规则！");
        }
        //查询案例对应的角色类型
        SportsProject sportsProject = getSportsProject(projectId);
        String roleType = sportsProject.getRoleType();
        String caseType = sportsProject.getCaseType();
        if ("".equals(year) || year == null) {
            Calendar calendar = Calendar.getInstance();
            year = String.valueOf(calendar.get(Calendar.YEAR));
            log.info("当前年为：{}", year);
        }
        String redisKey = CommonConstant.REDIS_JUDGE_RULE_BY_;

        if (CommonConstant.ROLE_TYPE_TE.equals(roleType)){
            redisKey  = redisKey + roleType + ":" + caseType;
        }else {
            redisKey  = redisKey + roleType;
        }

        List<SportsScoreRule> ruleList = (List<SportsScoreRule>) redisUtil.get(redisKey);
        if (null == ruleList || ruleList.size() == 0) {
            LambdaQueryWrapper<SportsScoreRule> lambdaQueryWrapper = getSportsScoreRuleList(roleType, year);
            if (CommonConstant.ROLE_TYPE_TE.equals(roleType)){
                lambdaQueryWrapper.eq(SportsScoreRule::getCaseType, caseType);
            }
            lambdaQueryWrapper.eq(SportsScoreRule::getRoleType, roleType);
            lambdaQueryWrapper.orderByAsc(SportsScoreRule::getId);
            ruleList = sportsScoreRuleMapper.selectList(lambdaQueryWrapper);
            redisUtil.set(redisKey, ruleList, JwtUtil.EXPIRE_TIME * 24);
        }

        map.put("ruleList", ruleList);
        String internationTravelRuleRedisKey = CommonConstant.INTERNATIONAL_TRAVEL_REDIS_JUDGE_RULE_BY_;
        List<SportsScoreRule> internationTravelRuleList = (List<SportsScoreRule>) redisUtil.get(internationTravelRuleRedisKey);
        if (CommonConstant.ROLE_TYPE_TE.equals(roleType) || CommonConstant.ROLE_TYPE_SM.equals(roleType)) {
            if (null == internationTravelRuleList || internationTravelRuleList.size() == 0) {
                LambdaQueryWrapper<SportsScoreRule> lambdaQueryWrapper = getSportsScoreRuleList(CommonConstant.ROLE_TYPE_INTERNATIONAL_TRAVEL, year);
                lambdaQueryWrapper.orderByAsc(SportsScoreRule::getId);
                internationTravelRuleList = sportsScoreRuleMapper.selectList(lambdaQueryWrapper);
                redisUtil.set(internationTravelRuleRedisKey, internationTravelRuleList, JwtUtil.EXPIRE_TIME * 24);
            }
        }
        map.put("internationTravelRuleList", internationTravelRuleList);
        return Result.OK("success", map);
    }

    public LambdaQueryWrapper <SportsScoreRule> getSportsScoreRuleList(String roleType, String year) {
        LambdaQueryWrapper<SportsScoreRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsScoreRule::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsScoreRule::getRoleType, roleType);
        lambdaQueryWrapper.eq(SportsScoreRule::getYear, year);
        return lambdaQueryWrapper;
    }

    public SportsProject getSportsProject(String projectId){
        LambdaQueryWrapper<SportsProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SportsProject::getIzActive, CommonConstant.COMMON_STATUS_N);
        lambdaQueryWrapper.eq(SportsProject::getId, projectId);
        return sportsProjectMapper.selectOne(lambdaQueryWrapper);
    }
}
